<?php

namespace App\Http\Controllers\Cruise\Web;

use App\Http\Controllers\BaseController;
use App\Models\Cruise\CruiseCheckAgainstModel;
use App\Models\Cruise\CruiseCheckErrorModel;
use App\Service\BaseService;
use App\Service\Cruise\Check\CruiseCheckErrorsService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Validation\ValidationException;

/**
 * 异常报备单
 */
class CruiseCheckErrorsController extends BaseController
{
    /**
     * 列表
     * @param Request $request
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function lists(Request $request): JsonResponse
    {
        $startTime   = (string)$request->get('startTime', '');
        $endTime     = (string)$request->get('endTime', '');
        $invalidType = (int)$request->get('invalidType', '');
        $dealUid     = (int)$request->get('cruiseName', 0);
        $pageSize    = (int)$request->get('pageSize', 20);
        $pageNum     = (int)$request->get('pageNum', 1);
        $status      = (string)$request->get('status', '');

        return $this->success(CruiseCheckErrorsService::lists($startTime, $endTime, $invalidType, $dealUid, $pageSize, $pageNum, $status));
    }


    /**
     * 详情
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws GuzzleException
     */
    public function detail(Request $request, int $id): JsonResponse
    {
        return $this->success(CruiseCheckErrorsService::detail($id));
    }

    /**
     * 处理
     * @param Request $request
     * @param int $id
     * @return JsonResponse
     * @throws ValidationException
     */
    public function edit(Request $request, int $id): JsonResponse
    {
        $request = $request->merge(['id' => $id]);

        $this->validate(
            $request, [
                'id'           => 'required|integer',
                'dealResult'   => 'required|integer|between:1,2',
            ]
        );
        $dealResult = (int)$request->get('dealResult', 0);
        $price = (int)$request->get('price', 0);
        if ($dealResult == CruiseCheckErrorModel::DEAL_RESULT_BREACH_LETTER) {//开具违约函
            $this->validate(
                $request, [
                    'level'        => 'required|integer|between:1,2',
                    'type'         => 'required|string',
                    'behaviorDesc' => 'required|string|min:0|max:255',
                    'rectifyPlan'  => 'required|string|min:0|max:255',
                ]
            );
            $type  = (string)$request->get('type', '');
            $type  = explode(',', $type);
            if (!$type || !is_array($type)) {
                return BaseService::throwErr("type不能为空");
            }
            if ($request->level == CruiseCheckAgainstModel::LEVEL_PUNISH && $price == 0) {//处罚才有金额
                return BaseService::throwErr("金额不能为0");
            }
        }
        CruiseCheckErrorsService::edit($id, $request, $price);

        return $this->success();
    }


}
